package com.example.weli.ble2test;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class JoystickView extends View {
    Boolean fPressed;
    private int fX;
    private int fY;
    private Drawable mJoystickBackground;
    private Drawable mJoystickNormal;
    private Drawable mJoystickPressed;
    private Paint mLabelPaint;
    private String mLabelText;

    public JoystickView(Context context) {
        super(context);
        initJoystickView(context);
    }

    public JoystickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initJoystickView(context);
    }

    public void setPosition(int x, int y) {
        this.fX = x;
        this.fY = y;
        this.mLabelText = String.format("%d,%d", new Object[]{Integer.valueOf(this.fX), Integer.valueOf(this.fY)});
        invalidate();
    }

    @Override
    public void setPressed(boolean pressed) {
        this.fPressed = Boolean.valueOf(pressed);
        invalidate();
    }

    private void initJoystickView(Context context) {
        this.fY = 0;
        this.fX = 0;
        this.fPressed = Boolean.valueOf(false);
        Resources r = context.getResources();
        this.mJoystickBackground = r.getDrawable(R.drawable.joystick_background);
        int w = this.mJoystickBackground.getIntrinsicWidth();
        int h = this.mJoystickBackground.getIntrinsicHeight();
        this.mJoystickBackground.setBounds(0, 0, w, h);
        this.mJoystickPressed = r.getDrawable(R.drawable.joystick_pressed_holo_dark);
        this.mJoystickNormal = r.getDrawable(R.drawable.joystick_normal_holo_dark);
        Utilities.centerAround((w / 2) - 4, (h / 2) + 4, this.mJoystickNormal);
        Utilities.centerAround((w / 2) - 4, (h / 2) + 4, this.mJoystickPressed);
        this.mLabelPaint = new Paint();
        this.mLabelPaint.setColor(-1);
        this.mLabelPaint.setTextSize(24.0f);
        this.mLabelPaint.setAntiAlias(true);
        this.mLabelPaint.setShadowLayer(1.0f, 2.0f, 2.0f, -16777216);
        setPosition(0, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.mJoystickBackground.draw(canvas);
        Drawable indicator = this.fPressed.booleanValue() ? this.mJoystickPressed : this.mJoystickNormal;
        int x = ((this.mJoystickBackground.getIntrinsicWidth() / 2) - 4) + this.fX;
        int y = ((this.mJoystickBackground.getIntrinsicHeight() / 2) + 4) + this.fY;
        Utilities.centerAround(x, y, indicator);
        indicator.draw(canvas);
        canvas.drawText(this.mLabelText, (float) (x + 12), (float) (y + 8), this.mLabelPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(this.mJoystickBackground.getIntrinsicWidth(), this.mJoystickBackground.getIntrinsicHeight());
    }
}
